package bibbletex.helpers;

import java.util.ArrayList;
import java.util.UUID;

import bibbletex.models.Reference;

public class BibtexFormat {

	public static ArrayList<Reference> toReference(String string) {
		char[] chars = string.toCharArray();
		
		int pointer = 0;
		ArrayList<Reference> EndResult = new ArrayList<Reference>();
			
		
		for (int i = 0; i < chars.length; i++) {
			if (chars[i] == '@') {
				pointer = i + 1;
				i = chars.length;
			}
		}
		while(pointer<chars.length){
			
			StringBuilder type = new StringBuilder();
			for (int i = pointer; i < chars.length; i++) {
				if (chars[i] != '{' && chars[i] != ' ') {
					type.append(chars[i]);
				} else if (chars[i] == '{') {
					pointer = i + 1;
					i = chars.length;
				}
			}
			Reference result = new Reference(UUID.randomUUID());
			result.setType(type.toString().toUpperCase());
	
			StringBuilder label = new StringBuilder();
			for (int i = pointer; i < chars.length; i++) {
				if (chars[i] != ',' && chars[i] != ' ') {
					label.append(chars[i]);
				} else if (chars[i] == ',') {
					pointer = i + 1;
					i = chars.length;
				}
			}
			result.setLabel(label.toString());
	
			while (true) {
				while (chars[pointer] == ' ' || chars[pointer] == '\u00A0' ||chars[pointer] == '\t' ||chars[pointer] == '\n' ||chars[pointer] == '\r') {
					pointer++;
				}
	
				StringBuilder attributeName = new StringBuilder();
				for (int i = pointer; i < chars.length; i++) {
					if (chars[i] != '=' && chars[i] != ' ') {
						attributeName.append(chars[i]);
					} else if (chars[i] == '=') {
						pointer = i + 1;
						i = chars.length;
					}
				}
	
				while (chars[pointer] != '{') {
					pointer++;
				}
	
				pointer++;
	
				StringBuilder attributeValue = new StringBuilder();
				for (int i = pointer; i < chars.length; i++) {
					if (chars[i] != '}'&& chars[i] != '\n') {
						attributeValue.append(chars[i]);
					} else if (chars[i] == '}') {
						pointer = i + 1;
						i = chars.length;
					}
				}
	
				if (attributeName.toString().equals("title")) {
					result.setTitle(attributeValue.toString());
				} else if (attributeName.toString().equals("author")) {
					result.setAuthors(attributeValue.toString());
				} else if (attributeName.toString().equals("booktitle")) {
					result.setBooktitle(attributeValue.toString());
				} else if (attributeName.toString().equals("editor")) {
					result.setEdition(attributeValue.toString());
				} else if (attributeName.toString().equals("publisher")) {
					result.setPublisher(attributeValue.toString());
				} else if (attributeName.toString().equals("organization")) {
					result.setOrganization(attributeValue.toString());
				} else if (attributeName.toString().equals("school")) {
					result.setSchool(attributeValue.toString());
				} else if (attributeName.toString().equals("journal")) {
					result.setJournal(attributeValue.toString());
				} else if (attributeName.toString().equals("year")) {
					result.setYear(attributeValue.toString());
				} else if (attributeName.toString().equals("volume")) {
					result.setVolume(attributeValue.toString());
				} else if (attributeName.toString().equals("number")) {
					result.setNumber(attributeValue.toString());
				} else if (attributeName.toString().equals("pages")) {
					result.setPages(attributeValue.toString());
				} else if (attributeName.toString().equals("month")) {
					result.setMonth(attributeValue.toString());
				} else if (attributeName.toString().equals("note")) {
					result.setNote(attributeValue.toString());
				} else if (attributeName.toString().equals("series")) {
					result.setSeries(attributeValue.toString());
				} else if (attributeName.toString().equals("address")) {
					result.setAddress(attributeValue.toString());
				} else if (attributeName.toString().equals("edition")) {
					result.setEdition(attributeValue.toString());
				} else if (attributeName.toString().equals("isbn")) {
					result.setIsbn(attributeValue.toString());
				} else if (attributeName.toString().equals("howpublished")) {
					result.setHowpublished(attributeValue.toString());
				} else {
					System.out.println("unknown attribute!");
				}
	
				while (chars[pointer] == ' ' || chars[pointer] == ',' || chars[pointer] == '\u00A0' ||chars[pointer] == '\n' ||chars[pointer] == '\t' ) {
					pointer++;
				}
	
				if (chars[pointer] == '}') {
					while (chars[pointer]!='@' && pointer<chars.length-1) {
						pointer++;
						
					}
					if(pointer<chars.length){
						pointer++;
					}
					
					break;
				}
				
			}
			EndResult.add(result);
		}
		return EndResult;
	}

	public static String toHTML(Reference r) {
		StringBuilder result = new StringBuilder();
		result.append('@');
		result.append(r.getType());
		result.append('{');
		result.append(r.getLabel());
		result.append(',');
		result.append("<br />");
		if (r.getTitle() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("title = {");
			result.append(r.getTitle());
			result.append("}, <br />");
		}
		if (r.getAuthors() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("author = {");
			result.append(r.getAuthors());
			result.append("}, <br />");
		}
		if (r.getBooktitle() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("booktitle = {");
			result.append(r.getBooktitle());
			result.append("}, <br />");
		}
		if (r.getEditor() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("editor = {");
			result.append(r.getEditor());
			result.append("}, <br />");
		}
		if (r.getPublisher() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("publisher = {");
			result.append(r.getPublisher());
			result.append("}, <br />");
		}
		if (r.getOrganization() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("organization = {");
			result.append(r.getOrganization());
			result.append("}, <br />");
		}
		if (r.getSchool() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("school = {");
			result.append(r.getSchool());
			result.append("}, <br />");
		}
		if (r.getJournal() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("journal = {");
			result.append(r.getJournal());
			result.append("}, <br />");
		}
		if (r.getYear() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("year = {");
			result.append(r.getYear());
			result.append("}, <br />");
		}
		if (r.getVolume() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("volume = {");
			result.append(r.getVolume());
			result.append("}, <br />");
		}
		if (r.getNumber() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("number = {");
			result.append(r.getNumber());
			result.append("}, <br />");
		}
		if (r.getPages() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("pages = {");
			result.append(r.getPages());
			result.append("}, <br />");
		}
		if (r.getMonth() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("month = {");
			result.append(r.getMonth());
			result.append("}, <br />");
		}
		if (r.getNote() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("note = {");
			result.append(r.getNote());
			result.append("}, <br />");
		}
		if (r.getSeries() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("series = {");
			result.append(r.getSeries());
			result.append("}, <br />");
		}
		if (r.getAddress() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("address = {");
			result.append(r.getAddress());
			result.append("}, <br />");
		}
		if (r.getEdition() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("edition = {");
			result.append(r.getEdition());
			result.append("}, <br />");
		}
		if (r.getIsbn() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("isbn = {");
			result.append(r.getIsbn());
			result.append("}, <br />");
		}
		if (r.getHowpublished() != null) {
			result.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			result.append("howpublished = {");
			result.append(r.getHowpublished());
			result.append("}, <br />");
		}
		result.append('}');
		return result.toString();
	}

	public static String toString(Reference r) {
		StringBuilder result = new StringBuilder();
		result.append('@');
		result.append(r.getType());
		result.append('{');
		result.append(r.getLabel());
		result.append(',');
		if (r.getTitle() != null) {
			result.append("title = {");
			result.append(r.getTitle());
			result.append("},");
		}
		if (r.getAuthors() != null) {
			result.append("author = {");
			result.append(r.getAuthors());
			result.append("},");
		}
		if (r.getBooktitle() != null) {
			result.append("booktitle = {");
			result.append(r.getBooktitle());
			result.append("},");
		}
		if (r.getEditor() != null) {
			result.append("editor = {");
			result.append(r.getEditor());
			result.append("},");
		}
		if (r.getPublisher() != null) {
			result.append("publisher = {");
			result.append(r.getPublisher());
			result.append("},");
		}
		if (r.getOrganization() != null) {
			result.append("organization = {");
			result.append(r.getOrganization());
			result.append("},");
		}
		if (r.getSchool() != null) {
			result.append("school = {");
			result.append(r.getSchool());
			result.append("},");
		}
		if (r.getJournal() != null) {
			result.append("journal = {");
			result.append(r.getJournal());
			result.append("},");
		}
		if (r.getYear() != null) {
			result.append("year = {");
			result.append(r.getYear());
			result.append("},");
		}
		if (r.getVolume() != null) {
			result.append("volume = {");
			result.append(r.getVolume());
			result.append("},");
		}
		if (r.getNumber() != null) {
			result.append("number = {");
			result.append(r.getNumber());
			result.append("},");
		}
		if (r.getPages() != null) {
			result.append("pages = {");
			result.append(r.getPages());
			result.append("},");
		}
		if (r.getMonth() != null) {
			result.append("month = {");
			result.append(r.getMonth());
			result.append("},");
		}
		if (r.getNote() != null) {
			result.append("note = {");
			result.append(r.getNote());
			result.append("},");
		}
		if (r.getSeries() != null) {
			result.append("series = {");
			result.append(r.getSeries());
			result.append("},");
		}
		if (r.getAddress() != null) {
			result.append("address = {");
			result.append(r.getAddress());
			result.append("},");
		}
		if (r.getEdition() != null) {
			result.append("edition = {");
			result.append(r.getEdition());
			result.append("},");
		}
		if (r.getIsbn() != null) {
			result.append("isbn = {");
			result.append(r.getIsbn());
			result.append("},");
		}
		if (r.getHowpublished() != null) {
			result.append("howpublished = {");
			result.append(r.getHowpublished());
			result.append("},");
		}
		result.append('}');
		return result.toString();
	}
	
}
